<script setup>
import { getCategory, updateCategory, getPhotoCategory, saveCategory, delCategory } from '@/api/photo'
import { ref, onMounted } from 'vue'
import { useUserStore } from '@/stores/user'
import { ElMessage, ElMessageBox } from 'element-plus';
import { useRouter } from 'vue-router'
// 实例化仓库
const userStore = useUserStore()
// 相册分类信息
const categoryList = ref([])
const getPhotoCategoryList = async () => {
  const res = await getPhotoCategory()
  categoryList.value = res.data
}
const headers = ref({ 'Authorization': userStore.userInfo.token })
// 页面组件挂载之后立马执行
onMounted(() => {
  getPhotoCategoryList()
})
// 弹层效果
const isDialog = ref(false)
// 添加分类名称
const operation = () => {
  isUpdateAdd.value = '新增分类'
  isDialog.value = true
}
// 参数
const photoParams = ref({
  cover: '',
  name: ''
})

// 图片上传成功的回调
const handleAvatarSuccess = (response) => {
  // 文件上传成功返回头像url
  photoParams.value.cover = response.data
}
// 存储当前用户的id
const categoryId = ref(null)
// 存储是否是编辑还是修改
const isUpdateAdd = ref('')
// 修改
const onUpdate = async (id) => {
  isUpdateAdd.value = '修改分类'
  isDialog.value = true
  // 根据id查询回显数据
  const res = await getCategory(id)
  // 用于修改时的凭证
  categoryId.value = id
  photoParams.value = res.data
}
// 新增分类
const handleOk = async () => {
  let message = '新增成功'
  if (categoryId.value) {
    message = '修改成功'
    await updateCategory(categoryId.value, photoParams.value)
  } else {
    // 新增
    await saveCategory(photoParams.value)
  }
  getPhotoCategoryList()
  ElMessage.success(message)
  // 清除默认
  onClose()
}
// 删除
const onDel = (id) => {
  ElMessageBox.confirm("您确定要删除该相册分类吗", "删除相册分类",
    {
      confirmButtonText: '确认',
      cancelButtonText: '取消',
      type: 'warning'
    }
  ).then(async () => {
    // 开始删除
    await delCategory(id)
    // 提示用户
    ElMessage.success("删除成功")
    // 重新获取
    getPhotoCategoryList()
  })
}
// 弹层关闭
const onClose = () => {
  photoParams.value = {}
  isDialog.value = false
  categoryId.value = null
}
// 实例化路由
const route = useRouter()
// 点击跳转照片墙
const goToPhoto = (id, name) => {
  // 开始跳转
  route.push({ path: `/personage/photo/${id}`, query: { name } })
}
</script>
<template>
  <div class="div">
    <el-card class="label">
      <template #header>
        <div class="card">
          <!-- 标题 -->
          <div class="card-header">
            <span style="color: #131f2b;font-weight: bold;">相册管理</span>
          </div>
          <!-- 按钮区域 -->
          <div class="card-rignt">
            <el-button type="primary" style="width: 80px;margin-left: 1100px;" size="small" @click="operation">
              <i class="iconfont icon-xinzeng" style="font-size: 14px;"></i>
              <span style="margin-right: 28px;">新增分类</span>
            </el-button>
          </div>
        </div>
      </template>
      <!-- 卡片区域 -->
      <div class="category-box">
        <el-card v-for="item in categoryList" :key="item.id" style="max-width: 230px;max-height: 290px;" shadow="hover"
          class="category-card">
          <template #header>
            <div class="header-box">
              <div class="span">
                <span style="color: #409eff;">{{ item.name }}</span>
                <div class="caozuo">
                  <i class="iconfont icon-shanchu i1" v-permissionDirective @click="onDel(item.id)"></i>
                  <i class="iconfont icon-xiugai i2" @click="onUpdate(item.id)"></i>
                </div>
              </div>
            </div>
          </template>
          <div class="card-img-box" @click="goToPhoto(item.id, item.name)">
            <!-- 这里做一个判断 -->
            <img class="card-img" v-if="item.cover" :src="item.cover" style="width: 100%" />
            <img class="card-img" v-else
              src="http://127.0.0.1:9000/blog/admin-photo-/20240616/93e859fe90bd49f0b2a735d8369bd51favatar.jpg"
              style="width: 100%" />
          </div>
        </el-card>
      </div>
    </el-card>
  </div>
  <!-- 修改和添加的弹层 -->
  <el-dialog width="400px" v-model="isDialog" @close="onClose">
    <template #title>
      <span style="color:#409eff"> {{ isUpdateAdd }}</span>
    </template>
    <!-- 图片位置 -->
    <el-form-item label="相册封面" prop="" style="margin-top: 20px;">
      <el-upload :headers="headers" style=" height: 160px;width: 160px;border-radius: 4px;border: 1px solid #409eff;"
        :on-success="handleAvatarSuccess" action="/prod-api/admin/photo/fileUpload" :show-file-list="false">
        <!-- 文件上传接口 -->
        <img v-if="photoParams.cover" :src="photoParams.cover" class="avatar" />
        <el-button v-else
          style="background-color: #ffffff;border: none;margin:70px 0px 0px 63px;font-size: 26px;width: 15px;height: 15px;"><span
            style="color: #8597b4;">+</span></el-button>
      </el-upload>
    </el-form-item>
    <!-- 分类输入框 -->
    <el-form-item label="分类名称" prop="" style="margin-top: 10px">
      <el-input placeholder="请输入相册分类名称" v-model="photoParams.name" style="width: 200px;"></el-input>
    </el-form-item>
    <template #footer>
      <div class="dialog-footer">
        <el-button size="small" @click="onClose">取 消</el-button>
        <el-button size="small" type="primary" @click="handleOk" :loading="confirmLoading" v-permissionDirective>确
          定</el-button>
      </div>
    </template>
  </el-dialog>
</template>
<style lang="scss" scoped>
.label {
  width: 1325px;
  background-color: #fff;
  border: none;
  height: 675px;
  border-radius: 6px;

  .card {
    display: flex;

  }

  .category-box {
    display: flex;
    flex-wrap: wrap;

    .category-card {
      margin-left: 20px;
      margin-bottom: 6px;

      .card-img-box {
        width: 200px;
        height: 200px;

        .card-img {
          width: 200px;
          height: 200px;
          object-fit: cover;
        }
      }

      .header-box {
        display: flex;

        .span {
          .caozuo {
            position: relative;

            i {
              margin: 0;
              padding: 0;
              position: absolute;
              top: 70%;
              left: 20%;

            }

            .i1 {
              left: 178px;
              transform: translateY(-100%);
              font-size: 17px;
            }

            .i2 {
              left: 155px;
              transform: translateY(-100%);
            }

            .i1:hover {
              color: red;
            }

            .i2:hover {
              color: #e8a274
            }
          }
        }

      }
    }

    .category-card:hover {
      cursor: pointer;
      font-weight: bold;
    }
  }
}

.avatar {
  width: 160px;
  height: 160px;
  object-fit: cover;
}
</style>